//
//  RecentAccessViewController.m
//  WindRunner
//
//  Created by leeqb on 12/7/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "RecentAccessViewController.h"

@interface RecentAccessViewController ()
{
    NSMutableArray *_resultArray;
}
@end

@implementation RecentAccessViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self) {
        _resultArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSDictionary *requestData = @{@"userid":[AuthHelper getCurrentUser][@"id"]};
    FMRequestObject *requestObj = [[FMRequestObject alloc] initWithRUL:BASE_API_URL_WINDRUNNER
                                                             interface:@"visitlist"
                                                                params:requestData];
    [FMNetwork request:requestObj showMask:NO success:^(NSDictionary *data) {
        [_resultArray addObjectsFromArray:data[@"record"]];
        [_userListTable reloadData];
    } falied:^(NSError *error) {
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.title = LocalStringSetting(@"NAV_TITLE_RECENT_ACCESS");
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_resultArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecentAccessCell" forIndexPath:indexPath];
    UIImageView *iconView = (UIImageView *)[cell viewWithTag:1];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:2];
    UIView *genderAgeView = (UIView *)[cell viewWithTag:3];
    UILabel *ageLabel = (UILabel *)[genderAgeView viewWithTag:31];
    UIImageView *genderImageView = (UIImageView *)[genderAgeView viewWithTag:32];
    UILabel *distanceLabel = (UILabel *)[cell viewWithTag:4];
    UIButton *favoButton = (UIButton *)[cell viewWithTag:5];
    
    [iconView showInCircle];
    genderAgeView.layer.cornerRadius = 8;
    genderAgeView.layer.masksToBounds = YES;
    favoButton.accessibilityValue = [NSString stringWithFormat:@"%@", @(indexPath.row)];
    
    NSDictionary *rowData = _resultArray[indexPath.row];
    if(rowData) {
        nameLabel.text = rowData[@"nickname"];
        
        LEDownloadManager *downloadManager = [[LEDownloadManager alloc] initWithTarget:iconView];
        [downloadManager downLoadImageWithURL:rowData[@"headicon"] imagetype:@"ContactHeadIocns"];
        
        NSString *locationStringX = rowData[@"lastlocationX"];
        NSString *locationStringY = rowData[@"lastlocationY"];
        if(![locationStringX isEqual:[NSNull null]] && ![locationStringY isEqual:[NSNull null]]) {
            double locationX = [rowData[@"lastlocationX"] doubleValue];
            double locationY = [rowData[@"lastlocationY"] doubleValue];
            double distanceMeters = [FMCommonFun distanceBetweenOrderBy:CURR_COORDINATE_X
                                                                       :locationX
                                                                       :CURR_COORDINATE_Y
                                                                       :locationY];
            //NSLog(@"%.1f", distanceMeters);
            distanceLabel.text = [NSString stringWithFormat:@"%.1f", distanceMeters];
        } else {
            distanceLabel.text = @"0";
        }
        
        // 年龄
        NSString *strBirthTimestamp = [NSString formatString:rowData[@"birthday"]];
        long long birthTimestamp = 0;
        if(![strBirthTimestamp isEqualToString:@""]) {
            birthTimestamp = [rowData[@"birthday"] longLongValue];
            ageLabel.text = [NSString stringWithFormat:@"%@", @([FMCommonFun getYearIntervalFromTimestamp:birthTimestamp])];
        } else {
            ageLabel.text = @"0";
        }
        
        // 性别
        if([rowData[@"sex"] integerValue] == 0) { // 女
            genderImageView.image = [UIImage imageNamed:@"ic_female.png"];
        } else { // 男
            genderImageView.image = [UIImage imageNamed:@"ic_male.png"];
        }
        
        NSInteger favorstatus = [[NSString formatString:rowData[@"favorstatus"]] integerValue];
        if(favorstatus == 0) {
            favoButton.hidden = NO;
        }
    }
    
    return cell;
}

@end
